﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Token.Utils;
using System.Threading.Tasks;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 我的账单
    /// </summary>
    [AppAuthorize("Member")]
    public class MyBillController : GeneralController {
        private IBillService _iBillService;
        private ITokenStatisticsService _iTokenStatisticsService;
        private ITokenBillService _iTokenBillService;
        private ITokenTransferService _iTokenTransferService;
        private ISettingService _iSettingService;
        private IMerchantBankCardService _iMerchantBankCardService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iBillService"></param>
        /// <param name="iTokenStatisticsService"></param>
        /// <param name="iMerchantBankCardService"></param>
        /// <param name="iTokenBillService"></param>
        /// <param name="iTokenTransferService"></param>
        /// <param name="iSettingService"></param>
        public MyBillController(IBillService iBillService, ITokenStatisticsService iTokenStatisticsService,
                                IMerchantBankCardService iMerchantBankCardService, ITokenBillService iTokenBillService,
                                ITokenTransferService iTokenTransferService, ISettingService iSettingService) {
            _iBillService = iBillService;
            _iTokenStatisticsService = iTokenStatisticsService;
            _iTokenBillService = iTokenBillService;
            _iTokenTransferService = iTokenTransferService;
            _iSettingService = iSettingService;
            _iMerchantBankCardService = iMerchantBankCardService;
        }

        #region 我的账单

        [HttpGet]
        public ActionResult MyBill() {
            var model = _iBillService.GetByMerchant(UserId);
            var token = _iTokenStatisticsService.GetOrCreate(UserId);
            ViewBag.token = token;
            return View(model);
        }
        /// <summary>
        /// 提现
        /// </summary>
        /// <returns></returns>
        public ActionResult Transfer() {
            ViewBag.setting = _iSettingService.GetTokenSetting().token_base_setting;
            var bank = _iMerchantBankCardService.GetBankCardByMerchantId(UserId);
            return View(bank);
        }
        /// <summary>
        /// 提现
        /// </summary>
        /// <param name="type">类型1：分销，2：课程，3：货款，4：业绩</param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public JsonResult SaveTransfer(int type, decimal amount) {
            int result = _iTokenTransferService.ApplyTokenTransfer(UserId, type, amount);
            return Json(ResultToJson.Json(result >= 0 ? " 提交成功" : "提交失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion
        /// <summary>
        /// 钱包
        /// </summary>
        /// <returns></returns>
        public ActionResult Wallet() {
            var result = _iTokenTransferService.GetWallet(UserId);
            return View(result);
        }
        /// <summary>
        /// 提现记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Deposited() {
            return View();
        }
        /// <summary>
        /// 提现记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PartialViewResult _Deposited(WalletQuery query) {
            query.merchant_id = UserId;
            var result = _iTokenTransferService.GetDeposited(query);
            return PartialView(result);
        }
        /// <summary>
        /// 收益 1 分销 2 课程 3 店铺
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult WalletDetail(int type) {
            var result = _iTokenTransferService.GetWalletDetail(UserId, type);
            return View(result);
        }
        /// <summary>
        /// 收益详情
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PartialViewResult _WalletDetail(WalletQuery query) {
            query.merchant_id = UserId;
            var result = new PagedResult<token_wallet_bill_detail_dto>();
            if (query.type == 2) {
                result = _iTokenBillService.GetWalletBillDetails(query);
            } else {
                result = _iBillService.GetWalletBillDetails(query);
            }
            ViewBag.type = query.type;
            return PartialView(result);
        }
    }
}